<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文本框聚焦失焦</title>
    <style>
      input {
        color: grey;
      }
    </style>
  </head>
  <body>
    <input type="text" id="inputBox" value="请输入搜索关键字" />
    <script>
      //获取元素
      var inputBox = document.getElementById("inputBox");
      inputBox.onfocus = function (e) {
        // console.log(inputBox);
        if (e.target.value === "请输入搜索关键字") {
          e.target.value = "";
          e.target.style.color = "black";
        }
      };
      inputBox.onblur = function (e) {
        if (e.target.value === "") {
          e.target.value = "请输入搜索关键字";
          e.target.style.color = "grey";
        }
      };
    </script>
  </body>
</html>
